package com.fingard.dsp.bank.reqrespobj.statement.detail;

import com.fingard.dsp.bank.directbank.PaymentState;
import com.fingard.dsp.bank.reqrespobj.statement.RefundState;
import com.fingard.dsp.bank.reqrespobj.statement.TransState;
import com.fingard.text.StringHelper;
import com.fingard.xml.XmlTextWriter;

/**
 * 对账单明细
 */
public class RetAtsStatementDetail {
    /**
     * 交易时间(格式：yyyyMMddHHmmss)
     */
    public String busTime;

    /**
     * 批次号，用于批量代收付的对账单
     */
    public String reqBatchNO;

    /**
     * 支付流水号，必填
     */
    public String askNumber;

    /**
     * 渠道订单号，必填
     */
    public String channelOrderNo;

    /**
     * 银行订单号，必填
     */
    public String bankOrderNo;

    /**
     * 金额(单位：元)
     */
    public String amount;

    /**
     * 币种
     */
    public String cur;

    /**
     * 手续费(单位：元)
     */
    public String fee;

    /**
     * 对方账号
     */
    public String oppAct;

    /**
     * 对方户名
     */
    public String oppActName;

    /**
     * 对方银行
     */
    public String oppBank;

    /**
     * 账号类型，借记卡、信用卡等
     */
    public String cardType;

    /**
     * 交易结果，success表示支付成功，fail表示支付失败，paying表示支付中，其它则无效报异常
     */
    public TransState transState;

    /**
     * 交易结果描述
     */
    public String transMsg;

    /**
     * 终端代码，如pos机的识别代码
     */
    public String termCode;

    /**
     * 业务类型，如首期交费、续期交费
     */
    public String busType;

    /**
     * 单据号码，如投保单号
     */
    public String srcNote;

    /**
     * 入账日期，yyyyMMddHHmmss
     */
    public String postingDate;

    /**
     * 支付类型，如无磁无密，有磁有密
     */
    public String payWay;

    /**
     * 操作员类型
     */
    public String oprType;

    /**
     * 操作员代码
     */
    public String oprCode;

    /**
     * 交易的商品名称或用途等摘要信息
     */
    public String summary;

    /**
     * 备注
     */
    public String memo;

    /**
     * 支付商订单号
     */
    public String bkSeq;

    /**
     * 支付商退款单号
     */
    public String bkRefundSeq;

    /**
     * 退款单号
     */
    public String refundSeq;

    /**
     * 优惠金额
     */
    public String offAmt;

    /**
     * 退款金额
     */
    public String refundAmt;

    /**
     * 优惠退款金额
     */
    public String refundOffAmt;

    /**
     * 退款类型
     */
    public String refundType;

    /**
     * 退款状态
     */
    public RefundState refundState;

    /**
     * 退款申请时间
     */
    public String refundSentDate;

    /**
     * 退款成功时间
     */
    public String refundMadeDate;

    /**
     * 组织机构号
     */
    public String organizationId;

    /**
     * 商户实收金额，以元为单位
     */
    public String receiptAmount;

    /**
     * 买家实付金额，以元为单位
     */
    public String buyerPayAmount;

    /**
     * 商家优惠金额，以元为单位
     */
    public String mDiscountAmount;

    /**
     * 平台优惠金额，以元为单位
     */
    public String discountAmount;

    /**
     * 借贷标记(22：代扣, 27：代付)
     */
    public String cdFlag;

    /**
     * 对账单类型(1:借,2:贷)
     */
    public String billType;

    public void writeXE(XmlTextWriter p_writer) {
        p_writer.writeElementString("BusTime", busTime);
        if (StringHelper.hasAnyChar(reqBatchNO)) {
            p_writer.writeElementString("ReqBatchNO", reqBatchNO);
        }
        p_writer.writeElementString("AskNumber", askNumber);
        p_writer.writeElementString("Amount", amount);
        if (StringHelper.hasAnyChar(cur)) {
            p_writer.writeElementString("Cur", cur);
        }
        if (StringHelper.hasAnyChar(fee)) {
            p_writer.writeElementString("Fee", fee);
        }
        if (StringHelper.hasAnyChar(oppAct)) {
            p_writer.writeElementString("OppAct", oppAct);
        }
        if (StringHelper.hasAnyChar(oppActName)) {
            p_writer.writeElementString("OppActName", oppActName);
        }
        if (StringHelper.hasAnyChar(oppBank)) {
            p_writer.writeElementString("OppBank", oppBank);
        } else {
            p_writer.writeElementString("OppBank", "");
        }
        if (StringHelper.hasAnyChar(channelOrderNo)) {
            p_writer.writeElementString("ChannelOrderNo", channelOrderNo);
        }
        if (StringHelper.hasAnyChar(bankOrderNo)) {
            p_writer.writeElementString("BankOrderNo", bankOrderNo);
        }
        if (StringHelper.hasAnyChar(cardType)) {
            p_writer.writeElementString("CardType", cardType);
        }
        if (transState == TransState.Success) {
            p_writer.writeElementString("TransState", PaymentState.SUCCESS);
        } else if (transState == TransState.Fail) {
            p_writer.writeElementString("TransState", PaymentState.FAIL);
        } else if (transState == TransState.Paying) {
            p_writer.writeElementString("TransState", PaymentState.PAYING);
        } else if (transState == TransState.ToRefund) {
            p_writer.writeElementString("TransState", PaymentState.TOREFUND);
        } else {
            p_writer.writeElementString("TransState", PaymentState.EXCEPTION);
        }

        if (StringHelper.hasAnyChar(transMsg)) {
            p_writer.writeElementString("TransMsg", transMsg);
        }
        if (StringHelper.hasAnyChar(termCode)) {
            p_writer.writeElementString("TermCode", termCode);
        }
        if (StringHelper.hasAnyChar(busType)) {
            p_writer.writeElementString("BusType", busType);
        }
        if (StringHelper.hasAnyChar(srcNote)) {
            p_writer.writeElementString("SrcNote", srcNote);
        }
        if (StringHelper.hasAnyChar(postingDate)) {
            p_writer.writeElementString("PostingDate", postingDate);
        }
        if (StringHelper.hasAnyChar(payWay)) {
            p_writer.writeElementString("PayWay", payWay);
        }
        if (StringHelper.hasAnyChar(oprType)) {
            p_writer.writeElementString("OprType", oprType);
        }
        if (StringHelper.hasAnyChar(oprCode)) {
            p_writer.writeElementString("OprCode", oprCode);
        }
        if (StringHelper.hasAnyChar(summary)) {
            p_writer.writeElementString("Summary", summary);
        }
        if (StringHelper.hasAnyChar(memo)) {
            p_writer.writeElementString("Memo", memo);
        }

        /**支付商订单号*/
        if (StringHelper.hasAnyChar(bkSeq)) {
            p_writer.writeElementString("BkSeq", bkSeq);
        } else {
            p_writer.writeElementString("BkSeq", "");
        }
        /**优惠金额*/
        if (StringHelper.hasAnyChar(offAmt)) {
            p_writer.writeElementString("OffAmt", offAmt);
        }
        /**退款状态*/
        if (refundState == RefundState.RefundSuccess) {
            p_writer.writeElementString("RefundState", PaymentState.REFUND_SUCCESS);
        } else if (refundState == RefundState.RefundFail) {
            p_writer.writeElementString("RefundState", PaymentState.REFUND_FAIL);
        } else if (refundState == RefundState.RefundPaying) {
            p_writer.writeElementString("RefundState", PaymentState.REFUND_PAYING);
        } else {
            p_writer.writeElementString("RefundState", PaymentState.EXCEPTION);
        }
        /**退款类型*/
        if (StringHelper.hasAnyChar(refundType)) {
            p_writer.writeElementString("RefundType", refundType);
        }
        /**退款单号*/
        if (StringHelper.hasAnyChar(refundSeq)) {
            p_writer.writeElementString("RefundSeq", refundSeq);
        }
        /**支付商退款单号*/
        if (StringHelper.hasAnyChar(bkRefundSeq)) {
            p_writer.writeElementString("BkRefundSeq", bkRefundSeq);
        }
        /**退款金额*/
        if (StringHelper.hasAnyChar(refundAmt)) {
            p_writer.writeElementString("RefundAmt", refundAmt);
        }
        /**优惠退款金额*/
        if (StringHelper.hasAnyChar(refundOffAmt)) {
            p_writer.writeElementString("RefundOffAmt", refundOffAmt);
        }
        /**退款申请时间*/
        if (StringHelper.hasAnyChar(refundSentDate)) {
            p_writer.writeElementString("RefundSentDate", refundSentDate);
        }
        /**退款成功时间*/
        if (StringHelper.hasAnyChar(refundMadeDate)) {
            p_writer.writeElementString("RefundMadeDate", refundMadeDate);
        }
        /**组织机构号*/
        if (StringHelper.hasAnyChar(organizationId)) {
            p_writer.writeElementString("OrganizationId", organizationId);
        }
        /**商户实收金额，以元为单位*/
        if (StringHelper.hasAnyChar(receiptAmount)) {
            p_writer.writeElementString("ReceiptAmount", receiptAmount);
        }
        /**买家实付金额，以元为单位*/
        if (StringHelper.hasAnyChar(buyerPayAmount)) {
            p_writer.writeElementString("BuyerPayAmount", buyerPayAmount);
        }
        /**商家优惠金额，以元为单位*/
        if (StringHelper.hasAnyChar(mDiscountAmount)) {
            p_writer.writeElementString("MDiscountAmount", mDiscountAmount);
        }
        /**平台优惠金额，以元为单位*/
        if (StringHelper.hasAnyChar(discountAmount)) {
            p_writer.writeElementString("DiscountAmount", discountAmount);
        }
        /** 借贷标记 */
        if (StringHelper.hasAnyChar(cdFlag) || StringHelper.hasAnyChar(billType)) {
            if (StringHelper.hasAnyChar(cdFlag)) {
                p_writer.writeElementString("cdFlag", cdFlag);
                if ("22".equals(cdFlag)) {
                    p_writer.writeElementString("BillType", "2");
                } else {
                    p_writer.writeElementString("BillType", "1");
                }
            } else {
                p_writer.writeElementString("BillType", billType);
                if ("1".equals(billType)) {
                    p_writer.writeElementString("cdFlag", "27");
                } else {
                    p_writer.writeElementString("cdFlag", "22");
                }
            }
        }
    }
}